package com.bitoon.basketdudes.web;

import com.bitoon.web.utils.LocalizedText;
import com.opensymphony.xwork2.ActionContext;
//import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.interceptor.I18nInterceptor;
import com.opensymphony.xwork2.util.ValueStack;
//import java.util.Locale;
// java.util.Map;
import org.apache.log4j.Logger;
//import org.apache.struts2.dispatcher.mapper.ActionMapping;

public class Idioma /* extends ActionSupport */ extends BaseAction {

    private String lang;
    private Logger log = Logger.getLogger(Idioma.class);
    //protected Map session;
    private String urlR;

    public Idioma() {
        session = ActionContext.getContext().getSession();
    }

    public String getUrl() {
        String retorno = "";
        //Locale locale = ActionContext.getContext().getLocale();
        //Locale locale = ActionContext.getContext().setLocale(locale);

        /*
        retorno += "http://";
        retorno += locale;
        retorno += ".devel.bitoon.mad";
        log.debug("Hasta ahora: " + retorno);
         * 
         */

        try {

            log.debug("urlR: " + urlR);

            if (urlR.equals("/landing/")) {
                retorno = "/landing/";
            } else {
                //Map<String,Object> session = ActionContext.getContext().getSession();
                //retorno += (String)session.get("paginaRetornoAnt"); // 13.10.2011
                retorno += (String)session.get("paginaRetorno");
            }
        } catch (Exception e) {
            log.error("Error: ", e);
            retorno += "/";
        }

        log.debug("Se devuelve: " + retorno);
        
        return retorno;
    }

    @Override
    public String execute() {

        log.debug("Entrando en execute de idioma");

        //Map<String,Object> session = ActionContext.getContext().getSession();

        if (getLang() != null) { // Viene un idioma

            try {
                log.debug("Antes, en sesion: " + I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE + ": " + session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));

                // Poner idioma en la sesión propia y en I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE
                session.put("bd_lang", getLang());
                session.put(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE, getLang());

                // Ponerlo en el stackValue
                ValueStack valueStack = ActionContext.getContext().getValueStack();
                valueStack.set("stackLang", getLang());

                log.debug("Despues, en sesion: " + I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE + ": " + session.get(I18nInterceptor.DEFAULT_SESSION_ATTRIBUTE));

                // Resetear los textos de LocalizedText
                LocalizedText lt = LocalizedText.getInstance();
                log.debug(">>>> lt: " + lt);

            } catch (Exception e) {
                log.error("[ERROR][execute]: " , e);
            }
        }

        return "redirect";
    }

    public String getLang() {
        return lang;
    }

    public void setLang(String lang) {
        this.lang = lang;
    }

    @Override
    public String getUrlR() {
        return urlR;
    }

    @Override
    public void setUrlR(String urlR) {
        this.urlR = urlR;
    }
}
